home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.11 Nov 91 / Icon List source / Main.c < prev   
Encoding:
C/C++ Source or Header  |  1990-08-10  |  6.8 KB  |  286 lines  |  [TEXT/KAHL]

  1. /*    ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  2.     Icon Lister
  3.  
  4.     Demo the List Object.
  5.     |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */
  6.  
  7. /* |||||||||| defines |||||||||| */
  8. #define        ICONWINKIND            4242
  9.  
  10. #include    <oops.h>
  11.  
  12. #ifndef         CLASS_LISTOBJ
  13. #include    "CListObj.h"
  14. #endif
  15.  
  16. #ifndef         CLASS_ICONITEMOBJ
  17. #include    "CIconItemObj.h"
  18. #endif
  19.  
  20. /* |||||||||| Globals |||||||||| */
  21. int                        DoneFlag;
  22. EventRecord            TheEvent;
  23. Point                        TheMousePt;
  24. Rect                        DragRect;
  25. MenuHandle            AppleMenu;
  26. MenuHandle            FileMenu;
  27. MenuHandle            EditMenu;
  28.  
  29. /*    |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */
  30. main ( )
  31.     {
  32.     WindowPtr            whichWindow;
  33.     char                    key;
  34.     CListObj            *anIconList;
  35.     GrafPtr                oldPort;
  36.  
  37.     Initialize ( );
  38.     while ( !DoneFlag ) {
  39.  
  40.         GetNextEvent ( everyEvent, &TheEvent );
  41.         switch ( TheEvent.what ) {
  42.             case activateEvt:
  43.                 whichWindow = (WindowPtr) TheEvent.message;
  44.                 if ( ((WindowPeek) whichWindow)->windowKind == ICONWINKIND ) {
  45.                     anIconList = (CListObj *) GetWRefCon (whichWindow );
  46.                     anIconList->ActivateList ( TheEvent.modifiers & activeFlag );
  47.                     }
  48.                 break;
  49.             case updateEvt:
  50.                 whichWindow = (WindowPtr) TheEvent.message;
  51.                 if ( ((WindowPeek) whichWindow)->windowKind == ICONWINKIND ) {
  52.                     GetPort ( &oldPort );
  53.                     SetPort ( whichWindow );
  54.                     anIconList = (CListObj *) GetWRefCon (whichWindow );
  55.                     BeginUpdate ( whichWindow );
  56.                     anIconList->UpdateList ( whichWindow->visRgn );
  57.                     EndUpdate ( whichWindow );
  58.                     SetPort ( oldPort );
  59.                     }
  60.                 break;
  61.             case mouseDown:
  62.                 switch ( FindWindow ( TheEvent.where, &whichWindow ) ) {
  63.                     case inMenuBar:
  64.                         DoMenu ( MenuSelect ( TheEvent.where ) );
  65.                         break;
  66.                     case inSysWindow:
  67.                         SystemClick ( &TheEvent, whichWindow );
  68.                         break;
  69.                     case inDrag:
  70.                         if ( ((WindowPeek) whichWindow)->windowKind == ICONWINKIND ) {
  71.                             DragWindow ( whichWindow, TheEvent.where, &DragRect );
  72.                             }
  73.                         break;
  74.                     case inGoAway:
  75.                         if ( ((WindowPeek) whichWindow)->windowKind == ICONWINKIND ) {
  76.                             if ( TrackGoAway ( whichWindow, TheEvent.where ) ) {
  77.                                 CloseIconWindow ( whichWindow );
  78.                                 }
  79.                             }
  80.                         break;
  81.                     case inContent:
  82.                         if ( ((WindowPeek) whichWindow)->windowKind == ICONWINKIND ) {
  83.                             if ( whichWindow != FrontWindow ( ) ) SelectWindow ( whichWindow );
  84.                             else {
  85.                                 SetPort ( whichWindow );
  86.                                 TheMousePt = TheEvent.where;
  87.                                 GlobalToLocal ( &TheMousePt );
  88.                                 anIconList = (CListObj *) GetWRefCon (whichWindow );
  89.                                 anIconList->MouseInList ( TheMousePt, TheEvent.modifiers );
  90.                                 }
  91.                             }
  92.                         break;
  93.                     }
  94.                 break;
  95.             case autoKey:
  96.             case keyDown:
  97.                 key = (char) (TheEvent.message & charCodeMask);
  98.                 if ( TheEvent.modifiers & cmdKey ) DoMenu ( MenuKey ( key ) );
  99.                 break;
  100.             case nullEvent:
  101.                 SystemTask ( );
  102.                 break;
  103.             }
  104.         }
  105.     }
  106.  
  107. /* |||||||||||||||||||| */
  108. Initialize ( )
  109.     {
  110.     Handle                h;
  111.     Str255                volName;
  112.     
  113.     /*** init the mac rom stuff */
  114.     InitGraf ( &thePort );
  115.     InitFonts ();
  116.     InitWindows ();
  117.     InitMenus ();
  118.     TEInit ();
  119.     InitDialogs ( 0L );
  120.     InitCursor ();
  121.     FlushEvents ( everyEvent, 0 );
  122.     
  123.     MaxApplZone (); MoreMasters (); MoreMasters ();
  124.     GetNextEvent ( everyEvent, &TheEvent );
  125.     GetNextEvent ( everyEvent, &TheEvent );
  126.     GetNextEvent ( everyEvent, &TheEvent );
  127.     GetNextEvent ( everyEvent, &TheEvent );
  128.  
  129.     /*** set up a few globals ... */
  130.     DoneFlag = false;
  131.     DragRect = screenBits.bounds;
  132.  
  133.     /*** read in the menus */
  134.     AppleMenu = GetMenu ( 1000 );
  135.     InsertMenu ( AppleMenu, 0 );
  136.     AddResMenu ( AppleMenu, 'DRVR' );
  137.     FileMenu = GetMenu ( 1001 );
  138.     InsertMenu ( FileMenu, 0 );
  139.     EditMenu = GetMenu ( 1002 );
  140.     InsertMenu ( EditMenu, 0 );
  141.     DrawMenuBar ();
  142.     }
  143.  
  144. /* |||||||||||||||||||| */
  145. DoMenu ( long mResult )
  146.     {
  147.     int                theMenu, theItem, i;
  148.     Str255            aname, freestr, maxstr;
  149.     GrafPtr            curport;
  150.     TEHandle        te;
  151.     MenuHandle    new;
  152.     long                sz, type;
  153.     int                mark, *menuNum;
  154.     WindowPtr        aWin;
  155.     
  156.     theItem = LoWord ( mResult );
  157.     theMenu = HiWord ( mResult );
  158.     switch ( theMenu ) {
  159.         case 1000:
  160.             if ( theItem == 1 ) DoAbout ( );
  161.             else {
  162.                 GetItem ( AppleMenu, theItem, aname );
  163.                 GetPort ( &curport );
  164.                 OpenDeskAcc ( aname );
  165.                 SetPort ( curport );
  166.                 }
  167.             break;
  168.         case 1001:
  169.             switch ( theItem ) {
  170.                 case 1:
  171.                     OpenIconWindow ( );
  172.                     break;
  173.                 case 2:
  174.                     aWin = FrontWindow ( );
  175.                     if ( aWin != 0L && ((WindowPeek) aWin)->windowKind == ICONWINKIND )
  176.                         CloseIconWindow ( aWin );
  177.                     break;
  178.                 case 4:
  179.                     DoneFlag = true;
  180.                     break;
  181.                 }
  182.             break;
  183.         case 1002:
  184.             break;
  185.         }
  186.  
  187.     HiliteMenu(0);
  188.     }
  189.  
  190. /* |||||||||||||||||||| */
  191. OpenIconWindow ( )
  192.     {
  193.     Point                where;
  194.     SFReply            theFile;
  195.     SFTypeList        typs;
  196.     WindowPtr        theWin;
  197.     CListObj        *theList;
  198.     Rect                bounds;
  199.     int                fileRef;
  200.     
  201.     /*** get the file */
  202.     where.h = where.v = 42;
  203.     SFGetFile ( where, 0L, 0L, -1, typs, 0L, &theFile );
  204.     if ( theFile.good ) {
  205.     
  206.         /*** open the file */
  207.         SetVol ( NULL, theFile.vRefNum );
  208.         fileRef = OpenResFile (theFile.fName );
  209.         if ( fileRef < 0 ) {
  210.             StopAlert ( 1000, NULL );
  211.             return;
  212.             }
  213.  
  214.         /*** set up the window, start it's list object, fill it with icons */
  215.         theWin = GetNewWindow ( 1000, 0L, -1L);
  216.         ((WindowPeek) theWin)->windowKind = ICONWINKIND;
  217.         SetWTitle ( theWin, theFile.fName );
  218.         bounds = theWin->portRect;
  219.         theList = new ( CListObj );
  220.         theList->IList ( &bounds, 40, true, lOnlyOne, theWin );
  221.         theList->DrawingOn ( false );
  222.  
  223.         /*** fill the list with ICONs */
  224.         FillIconList ( fileRef, theList );
  225.         theList->DrawingOn ( true );
  226.         CloseResFile ( fileRef );
  227.         SetWRefCon ( theWin, (long) theList );
  228.         }
  229.     }
  230.  
  231. /* |||||||||||||||||||| */
  232. FillIconList ( int aFile, CListObj *aList )
  233.     {
  234.     int                    i, iconID, numIcons;
  235.     ResType                iconType;
  236.     Str255                iconName;
  237.     CIconItemObj        *anIcon;
  238.     Handle                iconHan;
  239.     
  240.     /*** get each icon, create a new list item object, add it to the list */
  241.     numIcons = Count1Resources ( 'ICON' );
  242.     for ( i = 1; i <= numIcons; ++i ) {
  243.         if ( (iconHan = Get1IndResource ( 'ICON', i )) != 0L ) {
  244.             GetResInfo ( iconHan, &iconID, &iconType, iconName );
  245.             
  246.             anIcon = new ( CIconItemObj );
  247.             anIcon->id = iconID;
  248.             BlockMove ( iconName, anIcon->name, 255L );
  249.             DetachResource ( iconHan );
  250.             anIcon->bits = iconHan;
  251.             
  252.             aList->Add ( anIcon );
  253.             }
  254.         }
  255.     }
  256.  
  257. /* |||||||||||||||||||| */
  258. CloseIconWindow ( WindowPtr theWin )
  259.     {
  260.     CListObj            *theIconList;
  261.     CIconItemObj        *anIcon;
  262.     int                    i, num;
  263.     
  264.     /*** throw away it's contents and close the window */
  265.     theIconList = (CListObj *) GetWRefCon ( theWin );
  266.  
  267.     /*** throw away the CIconList objects, most efficient to go from the end of the list to the start */
  268.     theIconList->DrawingOn ( false );
  269.     num = theIconList->NumObjs();
  270.     for ( i = num; i >0; --i ) {
  271.         anIcon = (CIconItemObj *) theIconList->GetIndObject ( i );
  272.         theIconList->Remove ( anIcon );
  273.         DisposHandle ( anIcon->bits );
  274.         delete ( anIcon );
  275.         }
  276.     theIconList->Dispose ( );
  277.     DisposeWindow ( theWin );
  278.     }
  279.  
  280. /* |||||||||||||||||||| */
  281. DoAbout ( )
  282.     {
  283.     Alert ( 4242, 0L );
  284.     }
  285.  
  286.